package com.youlu.campus.web.sign.service.impl;

import com.youlu.campus.base.base.LiveStatusEnum;
import com.youlu.campus.base.base.PageQueryRequest;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.base.live.entity.LiveInfo;
import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.sign.dto.LiveResponse;
import com.youlu.campus.web.sign.dto.LiveSignRequest;
import com.youlu.campus.web.sign.service.LiveService;
import com.youlu.campus.web.common.utils.DateTimeUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 直播信息接口实现类
 * @author: YangJ
 * @date: 2020/12/10 15:17
 */
@Service("liveService")
public class LiveServiceImpl extends BaseService implements LiveService {


    @Override
    public Page<LiveResponse> liveList(PageQueryRequest request) {

        Query query = new Query();
        query.addCriteria(Criteria.where("deleted").is(false));
        query.addCriteria(Criteria.where("show").is(true));
//        String st = DateTimeUtils.getDayDate()+" 23:59:59";
//        Date date = DateTimeUtils.StringToDate(st);
//        query.addCriteria(Criteria.where("startTime").lt(date));
        long count = mongoTemplate.count(query, LiveInfo.class);

        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "startTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "startTime");
        query.with(sort);
        List<LiveInfo> liveInfoList = mongoTemplate.find(query.with(pageRequest), LiveInfo.class);
        List<LiveResponse> list = liveInfoList.stream().map(liveInfo -> {
            LiveResponse response = mapperFacade.map(liveInfo, LiveResponse.class);
            response.setStatusStr(LiveStatusEnum.getName(response.getStatus()));
            return response;
        }).collect(Collectors.toList());
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public LiveResponse queryTodayLive(String signPlanId) {

        Query query = new Query();
        query.addCriteria(Criteria.where("deleted").is(false));
        query.addCriteria(Criteria.where("items.signPlanId").is(signPlanId));
        String date = DateTimeUtils.formatDate(new Date(), "yyyyMMdd");
        query.addCriteria(Criteria.where("startDate").is(date));
        LiveInfo liveInfo = mongoTemplate.findOne(query, LiveInfo.class);
        if(liveInfo != null){
            LiveResponse response = mapperFacade.map(liveInfo, LiveResponse.class);
            return response;
        }
        return null;
    }

    @Override
    public LiveResponse queryLive(String signPlanId, String date) {
        Query query = new Query();
        query.addCriteria(Criteria.where("deleted").is(false));
        query.addCriteria(Criteria.where("items.signPlanId").is(signPlanId));
        query.addCriteria(Criteria.where("startDate").is(date));
        LiveInfo liveInfo = mongoTemplate.findOne(query, LiveInfo.class);
        if(liveInfo != null){
            LiveResponse response = mapperFacade.map(liveInfo, LiveResponse.class);
            return response;
        }
        return null;
    }

    @Override
    public boolean checkLieWord(LiveSignRequest request) {

        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(request.getLiveId()));
        query.addCriteria(Criteria.where("word").is(request.getWord()));
        boolean exists = mongoTemplate.exists(query, LiveInfo.class);
        if(!exists){
            throw new BusinessException("无效口令，请重新输入!");
        }

        return true;
    }
}
